/**
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

OAA_CI.abbrev_lang = OAA_CI.abbrev_lang || {};

OAA_CI.abbrev_lang.langView = new OAA_CI.SidebarView('ID_TREECHILDREN_LANGUAGES');
OAA_CI.abbrev_lang.abbrevView  = new OAA_CI.SidebarView('ID_TREECHILDREN_ABBREVIATIONS');

OAA_CI.abbrev_lang.views = [OAA_CI.abbrev_lang.langView, OAA_CI.abbrev_lang.abbrevView];

OAA_CI.abbrev_lang.hideMeter = function () { OAA_CI.abbrev_lang.meter.hidden = true; };
OAA_CI.abbrev_lang.showMeter = function () { OAA_CI.abbrev_lang.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.abbrev_lang;

/**
 * getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 *       called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 */

OpenAjax.a11y.cache.LanguageItem.prototype.getDisplayProperties = function () {
  return [this.count, this.lang, '', ''];
};


OpenAjax.a11y.cache.AbbreviationItem.prototype.getDisplayProperties = function () {
  return [this.count, this.abbreviation_text, '', ''];
};

/**
 * getChildren
 *
 * @desc Add getChildren method to cache elements; called by
 *       populateTreeAsHierarchy and populateTreeAsTwoLevelList.
 *
 * @return List of child elements
 */

OpenAjax.a11y.cache.LanguageItem.prototype.getChildren = function () {
  return this.dom_elements;
};

OpenAjax.a11y.cache.AbbreviationItem.prototype.getChildren = function () {
  return this.dom_elements;
};


/**
 * @function onLoad
 *
 * @memberOf OAA_CI.abbrev_lang
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.abbrev_lang.onLoad = function () {

  var config = {
   rules      : true,
   attributes : true,
   properties : true,
   style      : false,
   events     : true
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.abbrev_lang.langView.addProperties(pt);  
  OAA_CI.abbrev_lang.abbrevView.addProperties(pt);
  
  OAA_CI.abbrev_nls_strings = document.getElementById("ID_STRINGBUNDLE_ABBREV_LANG");

  OAA_CI.abbrev_lang.meter = document.getElementById('ID_METER_ABBREV_LANG');
  OAA_CI.abbrev_lang.abbrevView.isInitialized = false;
  OAA_CI.abbrev_lang.abbrevView.showCacheItems();

};

/**
 * @function langView.showCacheItems
 *
 * @memberOf OAA_CI.abbrev_lang
 *
 * @desc in responce to the Language button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with language
 *       items as two level ree. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.abbrev_lang.langView.showCacheItems = function () {
  var view = OAA_CI.abbrev_lang.langView;

  if (!view.isInitialized) {
    // Save references
    view.tab = document.getElementById('ID_TAB_LANGUAGES');
    view.tree = document.getElementById('ID_TREE_LANGUAGES');
    view.cache = OAA_CI.ruleset.dom_cache.languages_cache;

    // Populate the tabpanel
    var language_items = view.cache.language_items;
    OAA_CI.populateTreeAsTwoLevelList(language_items, 'ID_TREECHILDREN_LANGUAGES');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function abbrevView.showCacheItems
 *
 * @memberOf OAA_CI.abbrev_lang
 *
 * @desc in responce to the Abbreviations button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with abbreviation
 *       items as two level ree. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.abbrev_lang.abbrevView.showCacheItems = function () {

  var view = OAA_CI.abbrev_lang.abbrevView;

  if (!view.isInitialized) {
    // Save references
    view.tab = document.getElementById('ID_TAB_ABBREVIATIONS');
    view.tree = document.getElementById('ID_TREE_ABBREVIATIONS');
    view.cache = OAA_CI.ruleset.dom_cache.abbreviations_cache;

    // Populate the tabpanel
    view.cache.sortAbbreviationItems(true);
    var abbreviation_items = view.cache.abbreviation_items;
    OAA_CI.populateTreeAsTwoLevelList(abbreviation_items, 'ID_TREECHILDREN_ABBREVIATIONS');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.abbrev_lang
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the updateItemSelectionDisplay method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.abbrev_lang.onItemSelectionChange = function (index) {

  var view = OAA_CI.abbrev_lang.views[index];
  view.updateItemSelectionDisplay();

};
